home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / env.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  3.6 KB  |  185 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: env.c,v 5.2 1992/11/10 08:18:25 panos Exp $" ;
  8.  
  9. #include <syslog.h>
  10. #include <string.h>
  11.  
  12. #include "misc.h"
  13.  
  14. #include "attr.h"
  15. #include "defs.h"
  16. #include "sconf.h"
  17.  
  18. void msg() ;
  19. void out_of_memory() ;
  20.  
  21. extern char **environ ;
  22.  
  23. env_h std_env ;                /* created from environ */
  24.  
  25.  
  26. status_e initenv()
  27. {
  28.     std_env = env_make( environ ) ;
  29.     return( ( std_env == NULL ) ? FAILED : OK ) ;
  30. }
  31.  
  32.  
  33. status_e setup_environ( scp, def )
  34.     struct service_config *scp ;
  35.     struct service_config *def ;
  36. {
  37.     struct environment *ep = ENV( scp ) ;
  38.     status_e make_env_with_strings() ;
  39.     status_e make_env_from_vars() ;
  40.     status_e update_env_with_strings() ;
  41.  
  42.     if ( ! SPECIFIED( scp, A_PASSENV ) )
  43.     {
  44.         if ( ! SPECIFIED( def, A_PASSENV ) )
  45.         {
  46.             if ( ! SPECIFIED( scp, A_ENV ) )
  47.             {
  48.                 ep->env_type = STD_ENV ;
  49.                 ep->env = std_env ;
  50.                 return( OK ) ;
  51.             }
  52.             else
  53.                 return( make_env_with_strings( ep, std_env, scp->env ) ) ;
  54.         }
  55.         else    /* SPECIFIED( def, A_PASSENV ) */
  56.         {
  57.             struct environment *dep = ENV( def ) ;
  58.  
  59.             if ( dep->env_type == NO_ENV &&
  60.                     make_env_from_vars( dep, std_env, def->passenv ) == FAILED )
  61.                 return( FAILED ) ;
  62.  
  63.             if ( ! SPECIFIED( scp, A_ENV ) )
  64.             {
  65.                 ep->env_type = DEF_ENV ;
  66.                 ep->env = dep->env ;
  67.                 return( OK ) ;
  68.             }
  69.             else
  70.                 return( make_env_with_strings( ep, dep->env, scp->env ) ) ;
  71.         }
  72.     }
  73.     else    /* SPECIFIED( scp, A_PASSENV ) */
  74.     {
  75.         if ( make_env_from_vars( ep, std_env, scp->passenv ) == FAILED )
  76.             return( FAILED ) ;
  77.  
  78.         if ( ! SPECIFIED( scp, A_ENV ) )
  79.             return( OK ) ;
  80.         else
  81.         {
  82.             if ( update_env_with_strings( ep->env, scp->env ) == FAILED )
  83.             {
  84.                 env_destroy( ep->env ) ;
  85.                 return( FAILED ) ;
  86.             }
  87.             return( OK ) ;
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93. /*
  94.  * Create a new environment from environ and env_strings
  95.  * env_strings contains strings of the form "var=value"
  96.  */
  97. PRIVATE status_e make_env_with_strings( ep, env, env_strings )
  98.     struct environment *ep ;
  99.     env_h env ;
  100.     pset_h env_strings ;
  101. {
  102.     env_h new_env ;
  103.     char *func = "make_env_with_strings" ;
  104.     status_e update_env_with_strings() ;
  105.  
  106.     if ( ( new_env = env_create( env ) ) == ENV_NULL )
  107.     {
  108.         out_of_memory( func ) ;
  109.         return( FAILED ) ;
  110.     }
  111.  
  112.     if ( update_env_with_strings( new_env, env_strings ) == FAILED )
  113.     {
  114.         env_destroy( new_env ) ;
  115.         return( FAILED ) ;
  116.     }
  117.  
  118.     ep->env_type = CUSTOM_ENV ;
  119.     ep->env = new_env ;
  120.     return( OK ) ;
  121. }
  122.  
  123.  
  124. PRIVATE status_e make_env_from_vars( ep, env, vars )
  125.     struct environment *ep ;
  126.     env_h env ;
  127.     pset_h vars ;
  128. {
  129.     env_h new_env ;
  130.     char *varname ;
  131.     register unsigned u ;
  132.     char *func = "make_env_from_vars" ;
  133.  
  134.     if ( ( new_env = env_create( ENV_NULL ) ) == ENV_NULL )
  135.     {
  136.         out_of_memory( func ) ;
  137.         return( FAILED ) ;
  138.     }
  139.     
  140.     for ( u = 0 ; u < pset_count( vars ) ; u++ )
  141.     {
  142.         varname = (char *) pset_pointer( vars, u ) ;
  143.         if ( env_addvar( new_env, env, varname ) == ENV_ERR )
  144.         {
  145.             switch ( env_errno )
  146.             {
  147.                 case ENV_EBADVAR:
  148.                     msg( LOG_ERR, func, "Unknown variable %s", varname ) ;
  149.                     break ;
  150.                 
  151.                 case ENV_ENOMEM:
  152.                     out_of_memory( func ) ;
  153.                     env_destroy( new_env ) ;
  154.                     return( FAILED ) ;
  155.             }
  156.         }
  157.     }
  158.  
  159.     ep->env_type = CUSTOM_ENV ;
  160.     ep->env = new_env ;
  161.     return( OK ) ;
  162. }
  163.  
  164.  
  165. PRIVATE status_e update_env_with_strings( env, strings )
  166.     env_h env ;
  167.     pset_h strings ;
  168. {
  169.     register unsigned u ;
  170.     char *func = "update_env_with_strings" ;
  171.  
  172.     for ( u = 0 ; u < pset_count( strings ) ; u++ )
  173.     {
  174.         char *p = (char *) pset_pointer( strings, u ) ;
  175.  
  176.         if ( env_addstr( env, p ) == ENV_ERR )
  177.         {
  178.             out_of_memory( func ) ;
  179.             return( FAILED ) ;
  180.         }
  181.     }
  182.     return( OK ) ;
  183. }
  184.  
  185.